/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.sppp;

import com.max.oti.system.model.Company;
import com.max.oti.system.model.Country;
import com.max.oti.system.service.interfac.ICompanyService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Alberto
 */
@ManagedBean
@SessionScoped
public class CompanyBean implements Serializable {

    
    List<Country> listCountry;

    public List<Country> getListCountry() {
        if(listCountry ==null)
        {
            try {
                listCountry= new ArrayList();//globalService.listHQL("FROM Country ct");
            } catch (Exception ex) {
                Logger.getLogger(CompanyBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
            
        return listCountry;
    }

    public void setListCountry(List<Country> listCountry) {
        this.listCountry = listCountry;
    }
    List<Company> listCompany;

    public List<Company> getListCompany() {
        if (listCompany == null) {
            try {
                listCompany = new ArrayList();//globalService.listHQL("FROM Company c");
            } catch (Exception ex) {
                Logger.getLogger(CompanyBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return listCompany;
    }
    
    
    public void setListCompany(List<Company> listCompany) {
        this.listCompany = listCompany;
    }
    private Company company = new Company();
    @ManagedProperty("#{companyService}")
    ICompanyService companyService;

    public Company getCompany() {
        return company;
    }

    public ICompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(ICompanyService companyService) {
        this.companyService = companyService;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void saveCompany() {
        if (company.getId() == null) {
            try {
                companyService.save(company);
                FacesUtil.addMessage("Registro con Éxito", null, FacesMessage.SEVERITY_INFO);
                listCompany.add(company);
                company = new Company(); //resetear campos

            } catch (Exception ex) {
                Logger.getLogger(CompanyBean.class.getName()).log(Level.SEVERE, null, ex);
                FacesUtil.addMessage("Error en el Registro", null, FacesMessage.SEVERITY_ERROR);
            }
        } else {
            try {
                companyService.update(company);
                FacesUtil.addMessage("Actualizado con Éxito", null, FacesMessage.SEVERITY_INFO);
                company = new Company(); //resetear campos
                listCompany=new ArrayList();//globalService.listHQL("From Company c");
            } catch (Exception ex) {
                Logger.getLogger(CompanyBean.class.getName()).log(Level.SEVERE, null, ex);
                FacesUtil.addMessage("Error en el Registro", null, FacesMessage.SEVERITY_ERROR);
            }
        }
        


    }
    public void deleteCompany(){
        try {
            companyService.delete(company);
            listCompany=new ArrayList();//globalService.listHQL("From Company c");
            company= new Company();
        } catch (Exception ex) {
            Logger.getLogger(CompanyBean.class.getName()).log(Level.SEVERE, null, ex);
            
        }
        
    }
}
